﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using SMTC.Model.TestCore;
using System.Data;
using SMTC.Dao;
using SMTC.Model.Model;
namespace SMTC.Dao.UserPower
{
    public class UserPowerDao:BaseDao
    {
        #region SQL语句
        private const string SQL_SELECT_AdduserGridViewBind = @"select UserID,UserName,UserPsw from dbo.SMT_UserInfo";
        private const string SQL_SELECT_GETUSER = @"select UserID,UserName,UserPsw from dbo.SMT_UserInfo
                                                   where UserName = @UserName
                                                   and UserPsw = @UserPsw";
        private const string SQL_INSERT_User = @"insert into dbo.SMT_UserInfo(UserName,UserPsw)
                                                  values(@UserName,@UserPsw)";
        private const string SQL_Delete_User = @"delete from dbo.SMT_UserInfo where UserID=@UserID";
        private const string SQL_Update_User = @"update dbo.SMT_UserInfo set UserName=@UserName,UserPsw=@UserPsw where  UserID=@UserID"; 
        private const string SQL_INSERT_News= @"insert into dbo.SMT_Message(PublishUser,PublishDateTime,PubLevel,NewsTitle,NewsContent,ReSUser)
                                                  values(@PublishUser,@PublishDateTime,@PubLevel,@NewsTitle,@NewsContent,@ReSUser)";
        private const string SQL_SELECT_News_Receiver_GridViewBind = @"select UserName from dbo.SMT_UserInfo ";
        private const string SQL_SELECT_NewsList_GridViewBind = @"select NewID,NewsTitle,PublishDateTime from dbo.SMT_Message where ReSUser=@ReSUser order by PublishDateTime desc";
        private const string SQL_Check_NewsList_GridViewBind = @"select NewId,ReSUser from dbo.SMT_Message";
        private const string SQL_SELECT_News_Detail= @"select PublishUser,PublishDateTime,PubLevel,NewsTitle,NewsContent,ReSUser from dbo.SMT_Message where NewID=@NewID";
        private const string SQL_Check_Receiver = @"select UserName from dbo.SMT_UserInfo where UserName=@UserName";
        private const string SQL_Search_Receiver = @"select UserName from dbo.SMT_UserInfo where UserName like @UserName";
        private const string SQL_Mounter_Maintenance = @"select MounterID,MounterType,MounterAbility,MounterParameter,MounterMaintenancer,MounterMaintenanceTime from dbo.MounterMaintenance";
        private const string SQL_Delete_Mounter_Maintenance = @"delete from dbo.MounterMaintenance where MounterID=@MounterID";
        private const string SQL_Insert_Mounter_Maintenance = @"insert into  dbo.MounterMaintenance(MounterType,MounterAbility,MounterParameter) values(@MounterType,@MounterAbility,@MounterParameter)";
        private const string SQL_Update_Mounter_Maintenance = @"update dbo.MounterMaintenance set MounterAbility=@MounterAbility,MounterParameter=@MounterParameter,MounterMaintenancer=@MounterMaintenancer,MounterMaintenanceTime=@MounterMaintenanceTime where  MounterID=@MounterID";
        private const string SQL_One_Mounter_Maintenance = @"select MounterType,MounterAbility,MounterParameter,MounterMaintenancer,MounterMaintenanceTime from dbo.MounterMaintenance where MounterID=@MounterID";
        private const string SQL_Search_Type_Mounter_Maintenance = @"select MounterID,MounterType,MounterAbility,MounterParameter,MounterMaintenancer,MounterMaintenanceTime from dbo.MounterMaintenance where MounterType=@value";
        /// <summary>
        /// 获取权限
        /// </summary>
        /// <returns></returns>
        /// 
        private const string SQL_Select_User_Function = @"select rf.FunctionID FunctionID
                                                        from  dbo.SMT_UserInfo u left join dbo.SMT_MemberInRole mr on u.UserID=mr.MemberID
                                                        left join dbo.SMT_RoleFunction rf on rf.RoleID=mr.RoleID
                                                        where u.UserID=@UserID group by u.UserID,rf.FunctionID";
        #endregion
     
        public List<SMT_UserInfo> AdduserGridAdduserGridViewBind()
        {
            List<SMT_UserInfo> userlist= DBTemplete.ExcuteGetList<SMT_UserInfo>(CommandType.Text, SQL_SELECT_AdduserGridViewBind);
            return userlist;
        }
        /// <summary>
        /// 验证用户是否存在-name，psw
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>如果存在返回true，否则返回false</returns>
        public bool ValidationUserPower(string userName, string passWord,ref SMT_UserInfo refuser)
        {
            SMT_UserInfo user = null;
            DbParameter[] parameter =
            {
                DBparameterBuild.Build("@UserName",userName)
               ,DBparameterBuild.Build("@UserPsw",passWord)
            };

            try
            {
                 user = DBTemplete.ExcuteGetObject<SMT_UserInfo>(CommandType.Text, SQL_SELECT_GETUSER, parameter);
                if (user != null && user.UserName == userName)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                refuser = user;
            }
        }
        public void InsertUser(string UserName, string UserPsw)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@UserName",UserName)
               ,DBparameterBuild.Build("@UserPsw",UserPsw)
              
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_INSERT_User, parameter);


            }
            catch (Exception ex)
            {

            }

        }
        public void DeleteUser(string  UserID)
        {
            DbParameter[] parameter =
            {
 
                DBparameterBuild.Build("@UserID",UserID)
              
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_Delete_User, parameter);


            }
            catch (Exception ex)
            {

            }

        }
        public void UpdateUser(string UserID, string UserName, string UserPsw)
        {
            DbParameter[] parameter =
            { 
                 DBparameterBuild .Build ("@UserID",UserID)
                ,DBparameterBuild.Build("@UserName",UserName)
                ,DBparameterBuild.Build("@UserPsw",UserPsw)
              
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_Update_User, parameter);


            }
            catch (Exception ex)
            {

            }

        }
      
        public void Insert_News(string PublishUser, string PublishDateTime, int PubLevel, string NewsTitle, string NewsContent, string ReSUser)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@PublishUser",PublishUser)
               ,DBparameterBuild.Build("@PublishDateTime",PublishDateTime)
               ,DBparameterBuild.Build("@PubLevel",PubLevel)
               ,DBparameterBuild.Build("@NewsTitle",NewsTitle)
               ,DBparameterBuild.Build("@NewsContent",NewsContent)
               ,DBparameterBuild.Build("@ReSUser",ReSUser)
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_INSERT_News, parameter);


            }
            catch (Exception ex)
            {

            }

        }

        public DataSet SELECT_News_Receiver_GridViewBind()
        {
            DataSet set = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_SELECT_News_Receiver_GridViewBind);
            return set;
        }

        public DataSet  SELECT_NewsList_GridViewBind(string ReSUser)
        {
           
            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@ReSUser",ReSUser)
             
             };

            DataSet set = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_SELECT_NewsList_GridViewBind, parameter);
                return set;
        }
       //checkssssssssssssssssssssssssssssssssssssssssssssssssssssssss
        public DataSet  Check_NewsList_GridViewBind()
        {
           DataSet  receiver =DBTemplete.ExcuteFillDataSet (CommandType.Text, SQL_Check_NewsList_GridViewBind);
            return receiver;

        }

        //checkssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
        public DataSet SELECT_News_Detail(string NewID)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@NewID",NewID)
             
             };

            DataSet set = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_SELECT_News_Detail, parameter);
            return set;
        }
        //检查收件人是否合法
        public string  Check_Receiver(string UserName)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@UserName",UserName)
             
             };

          string receiver = Convert.ToString(DBTemplete.ExecuteScalar(CommandType.Text,SQL_Check_Receiver, parameter));
          return receiver;
        
        }
        public DataSet Search_Receiver(string UserName)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@UserName",UserName)
             
             };
            DataSet set = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_Search_Receiver, parameter);
            return set;
           
        }
        public DataSet Mounter_Maintenance()
        {
            DataSet maintenance = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_Mounter_Maintenance);
            return maintenance;
        }
        public void Delete_Mounter_Maintenance(string MounterID)
        {
            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@MounterID",MounterID)
             
             };
            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_Delete_Mounter_Maintenance,parameter);


            }
            catch (Exception ex)
            {

            }
        }
        public void Insert_Mounter_Maintenance(string MounterType, int MounterAbility,string MounterParameter)
        {

            DbParameter[] parameter =
            {
               
               DBparameterBuild.Build("@MounterType",MounterType)
               ,DBparameterBuild.Build("@MounterAbility",MounterAbility)
               ,DBparameterBuild.Build("@MounterParameter",MounterParameter)
              
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_Insert_Mounter_Maintenance, parameter);


            }
            catch (Exception ex)
            {

            }

        }
        public void Update_Mounter_Maintenance(string MounterID, int MounterAbility, string MounterParameter,string MounterMaintenancer,string MounterMaintenanceTime)
        {
            //@MounterMaintenancer,MounterMaintenanceTime
            DbParameter[] parameter =
            {
               
                DBparameterBuild.Build("@MounterID",MounterID)
               ,DBparameterBuild.Build("@MounterAbility",MounterAbility)
               ,DBparameterBuild.Build("@MounterParameter",MounterParameter)
               ,DBparameterBuild.Build("@MounterMaintenancer",MounterMaintenancer)
               ,DBparameterBuild.Build("@MounterMaintenanceTime",MounterMaintenanceTime)
              
             };

            try
            {
                int result = DBTemplete.ExcuteNoQuery(System.Data.CommandType.Text, SQL_Update_Mounter_Maintenance, parameter);


            }
            catch (Exception ex)
            {

            }

        }
        public DataSet One_Mounter_Maintenance(string MounterID)
        {
            DbParameter[] parameter =
            {
              
               DBparameterBuild.Build("@MounterID",MounterID)
              
             };

            DataSet maintenance = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_One_Mounter_Maintenance, parameter);
            return maintenance;
        }
        //SQL_Search_Mounter_Maintenance
        public DataSet Search_Type_Mounter_Maintenance(string value)
        { 
            DbParameter[] parameter =
            {
              
              DBparameterBuild.Build("@value",value)
              
             };

            DataSet maintenance = DBTemplete.ExcuteFillDataSet(CommandType.Text, SQL_Search_Type_Mounter_Maintenance, parameter);
            return maintenance;
        }
        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<string> Get_UserFunction_List(string UserID)
        {
            DbParameter[] parameter =
            {
                DBparameterBuild.Build("@UserID",UserID)
             };

            return DBTemplete.ExcuteGetList<string>(CommandType.Text, SQL_Select_User_Function, parameter);

        }
    }
}
